package com.fbt.andon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbt.andon.domain.Line;
import com.fbt.andon.domain.Worker;
import com.fbt.andon.service.impl.WorkerServiceImpl;

import com.linkwechat.common.annotation.Log;
import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 人员Controller
 *
 * @author fengtao
 * @date 2021-08-27
 */
@RestController
@RequestMapping("/worker")
@Api(description = "人员信息api")
public class WorkerController extends BaseController {
    @Autowired
    private WorkerServiceImpl workerService;


    //@PreAuthorize(hasPermi = "mes:andon:query")
    @ApiOperation("查询所有人员列表")
    @GetMapping("/query")
    public AjaxResult list() {
        startPage();
        List<Worker> list = workerService.list(null);
        list.forEach(System.out::println);
        return AjaxResult.success("查询成功", list);
    }


    //@PreAuthorize(hasPermi = "mes:andon:query")
    @PostMapping("/findByWorkName/{workName}")
    @ApiOperation("根据工人名模糊查询")
    public AjaxResult findByWorkName(@PathVariable("workName") String workName) {
        startPage();
        System.out.println("===========" + workName);
        QueryWrapper<Worker> wrapper = new QueryWrapper<>();
        wrapper.like("work_name", workName);
        return AjaxResult.success("查询成功", workerService.list(wrapper));
    }

    //@PreAuthorize(hasPermi = "mes:andon:query")
    @PostMapping("/findByWorkerNumber/{workerNumber}")
    @ApiOperation("根据工人号模糊查询")
    public AjaxResult findByWorkerNumber(@PathVariable("workerNumber") String workerNumber) {
        startPage();
        QueryWrapper<Worker> wrapper = new QueryWrapper<>();
        wrapper.like("worker_number", workerNumber);
        return AjaxResult.success("查询成功", workerService.list(wrapper));
    }


    //@PreAuthorize(hasPermi = "mes:andon:query")
    @ApiOperation("根据id查询人员")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success("获取成功", workerService.getById(id));
    }


    //@PreAuthorize(hasPermi = "mes:andon:add")
    @ApiOperation("新增人员")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Worker worker) {
        System.out.println(worker);
        return AjaxResult.success("保存成功", workerService.save(worker));
    }


    //@PreAuthorize(hasPermi = "mes:andon:edit")
    @ApiOperation("修改人员")
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody Worker worker) {
        return AjaxResult.success("修改成功", workerService.updateById(worker));
    }


    //@PreAuthorize(hasPermi = "andon:worker:remove")
    @ApiOperation("批量删除人员")
    @PostMapping("/batchRemove/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return AjaxResult.success("删除成功", workerService.removeByIds(Arrays.asList(ids)));
    }


    /**
     * 导出产线列表
     */
    //@PreAuthorize(hasPermi = "mes:andon:export")
    @Log(title = "产线", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Worker worker) throws IOException {
        List<Worker> list = workerService.list(null);
        ExcelUtil<Worker> util = new ExcelUtil<Worker>(Worker.class);
      //  util.exportExcel(response, list, "line");
    }
}
